/*
 * 
 * 
 * 
 */
package com.hboxs.asl.dao.impl;

import com.hboxs.asl.dao.ShippingMethodDao;
import com.hboxs.asl.entity.Merchant;
import com.hboxs.asl.entity.ShippingMethod;
import org.springframework.stereotype.Repository;

import javax.persistence.FlushModeType;
import javax.persistence.NoResultException;

/**
 * Dao - 配送方式
 */
@Repository("shippingMethodDaoImpl")
public class ShippingMethodDaoImpl extends BaseDaoImpl<ShippingMethod, Long> implements ShippingMethodDao {

    @Override
    public ShippingMethod findDefault() {
        String jpql = "select shippingMethod from ShippingMethod shippingMethod where shippingMethod.isDefault = true and shippingMethod.merchant is null ";
        try {
            return entityManager.createQuery(jpql, ShippingMethod.class).setFlushMode(FlushModeType.COMMIT).getSingleResult();
        } catch (NoResultException e) {
            return null;
        }
    }

    @Override
    public ShippingMethod findDefault(Merchant merchant) {
        String jpql = "select shippingMethod from ShippingMethod shippingMethod where shippingMethod.isDefault = true and shippingMethod.merchant = :merchant";
        try {
            return entityManager.createQuery(jpql, ShippingMethod.class).setFlushMode(FlushModeType.COMMIT).setParameter("merchant", merchant).getSingleResult();
        } catch (NoResultException e) {
            return null;
        }
    }

}